<?php
/**
 * Created by PhpStorm.FUN-AC-0042-新年送红包活动
 * User: allen.qiang
 * Date: 2015-02-12
 * Time: 上午10:50
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityRedEnvelopes {

    public $activityConfigName="Red_Envelopes";
    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  2015-02-12 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master($pcustID){
        global $G_SHOP;

        //实例化配置类
        $activity_config = new activityConfig($this->activityConfigName); //新年红包活动配置
        $config_array = $activity_config->config;

        $isbool=false;
        //sql字符串
        $sql = ' ';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //会员ID
        if (isset($pcustID) && !empty($pcustID)) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 's';
            $params_array[] = $pcustID;
            $isbool=true;
        }

        $NewAndOld=0;//是否是新老用户
        $isRedEnvelopes=0;//是否已经领取红包
       if($isbool){
           //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<判断是否是新用户和老用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
           $sql_cust_master="SELECT COUNT(1) AS item_count  FROM `cust_master` WHERE createTime>'".$config_array["startTime"]."' ".$sql;
           $cust_master= &$G_SHOP->DBCA->stmt_query($sql_cust_master,$params_type,$params_array);
           $item_count = $cust_master[0]->item_count;
           if($item_count>0){ //新用户
               $NewAndOld=1;
           }
           //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<判断是否是新用户和老用户>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

           //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<判断用户是否已经领取红包>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
           $sql_activities_extend="SELECT COUNT(1) AS item_count FROM `activities_extend` WHERE activityNumber='Red_Envelopes' AND extend_1=? ";
           $activities_extend = &$G_SHOP->DBCA->stmt_query($sql_activities_extend,'s',$pcustID);
           $item_count2 = $activities_extend[0]->item_count;
           if($item_count2>0){ //已领取红包
               $isRedEnvelopes=1;
           }
           //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<判断用户是否已经领取红包>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

       }

        //用户信息数组
        $activity_array = array(
            'startTime'	 => $config_array['startTime'],//活动开始时间
            'endTime'	     => $config_array['endTime'],//活动结束时间
            'NewAndOld' => $NewAndOld,//是否是新老用户
            'isRedEnvelopes' => $isRedEnvelopes,//是否已经领取红包
            'EightID' => $config_array["EightID"], //8元红包ID
            'TwelveID' => $config_array["TwelveID"] //12元红包ID
        );

        return $activity_array;

    }


    /**
     * 新年新老用户领取红包
     * @author  2015-02-12 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function activity_red_envelopes() {
        global $G_SHOP;
        //引用优惠券类
        quanli_require('module/base/promotion/quanliCoupon.class.php');
        $pcustID=$G_SHOP->currentPCustID;//会员ID

        //实例化新年红包活动类
        $activityRedEnvelopes=$this->activity_master($pcustID);
        if($activityRedEnvelopes['isRedEnvelopes']==1){ //已领取红包
            return array( 'code' => 300, 'message' => '您已经领取了新年红包' );
        }
        $couponID=$activityRedEnvelopes['EightID'];//老用户
        $yuan=12;
        if($activityRedEnvelopes['NewAndOld']==1){ //新用户
            $couponID=$activityRedEnvelopes['TwelveID'];
            $yuan=28;
        }

        //实例化优惠券类
        $ebsigCoupon = new ebsigCoupon( $G_SHOP->DBCA );

        //发放红包
        $result_array = $ebsigCoupon->createCouponCode( $couponID, array( $G_SHOP->currentCustomerID ) );
        if($result_array['code']==200)
        {
            //保存活动信息
            $activities_extend = new entity('activities_extend');
            $activities_extend->creator = $G_SHOP->currentCustomerID;
            $activities_extend->activityNumber =$this->activityConfigName;
            $activities_extend->classifyID = $couponID;
            $activities_extend->extend_1 = $pcustID;
            $activities_extend->extend_2 = date('Y-m-d H:i:s');
            $activities_extend->extend_4 = $activityRedEnvelopes['NewAndOld'];
            $activities_extend->extend_5 = $yuan;
            $G_SHOP->DBCA->save( $activities_extend );
        }else{
            return array( 'code' => 400, 'message' => $result_array['message'] );
        }

        return array( 'code' => 200, 'message' => '成功领取新年红包' );

    }


} 